from openpyxl import Workbook, load_workbook
wb = load_workbook('books.xlsx')
ws = wb.active
print(ws)
#輸出為:<Worksheet "Sheet">
print(ws['A10'].value)
#輸出:電力電子乙級技能檢定學術科試題解析|2025版
wb = Workbook()
ws = wb.active()
ws.title = 'python'
ws.save('excel.xlsx')
from openpyxl import Workbook, load_workbook
wb = Workbook()
ws = wb.active
ws.title = 'python'
ws.append(['A', 'B', 'C', 'D', 'E'])
wb.save('excel.xlsx')
wb.save('excel.xlsx')再執行一次的話,會覆蓋掉前面的,重新打開後如下:
ws.merge_cells('A1:C1')
ws.unmerge_cells('A1:C1')
ws.insert_rows(3)
ws.insert_cols(3)
ws.insert_rows(3)
ws.insert_cols(3)
ws.move_range('A3:E4', rows=2, cols=1)#往下兩排、往右一行
openpyxl更多使用方法:openpyxl